﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Ctrls;
using CommunitySystem.Views;
using System.Linq;
using CommunitySystem.PageView;
using System.Collections.ObjectModel;
using CommunitySystem.JinShengService;
using CommunitySystem.PageView.晋升管理.新的晋升管理;
using EHR.Common;
using System.IO;

namespace CommunitySystem
{
    public partial class CandidatorInfoControl : AsyncCtrl, IReturnListenerc
	{
        private JinShengService.JinShengServiceClient _client = null;
        private JinShengService.JinShen_MainInfo _MainInfo = null;
        public CandidatorInfoControl(JinShengService.JinShen_MainInfo mainInfo)
		{
			// 为初始化变量所必需
			InitializeComponent();

            IsWorking.Visibility = Visibility.Visible;

            _MainInfo = mainInfo;
            _client = new JinShengService.JinShengServiceClient();
            _client.GetDemocraEvalutionCompleted += (sender, e) =>
            {
                var eval =  e.Result.FirstOrDefault();

                TextBlock tb = new TextBlock() { Text = "科室:" + mainInfo.Department, Margin = new Thickness(10, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);
                tb = new TextBlock() { Text = "职位:" + mainInfo.Position + "(" + mainInfo.PositionLevel+ ")", Margin = new Thickness(60, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);

                 tb = new TextBlock() { Text = "应参加测评人数:", Margin = new Thickness(60, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);
                tb = new TextBlock() { Text = eval.TotalCount + "", VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);
                tb = new TextBlock() { Text = "实际加测评人数:", Margin = new Thickness(60, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);
                tb = new TextBlock() { Text = eval.Realcount + "", VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);
                int index = 1;
                foreach (var Evaldetail in eval.Details)
                {
                    var detail = _MainInfo.Details.Where(c => c.EmpBasicID == Evaldetail.EmpID).FirstOrDefault();
                    if (detail != null)
                    {
                        StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(5, 10, 0, 0), Height = 30, VerticalAlignment = System.Windows.VerticalAlignment.Top, DataContext = detail, Tag = Evaldetail };
                        tb = new TextBlock() { Text = detail.Name, Margin = new Thickness(0, 0, 10, 0), Width=35, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        if (index % 2 == 1)
                            sp.Background = new SolidColorBrush(Colors.LightGray);
                        index++;
                        sp.Children.Add(tb);
                        tb = new TextBlock() { Text = detail.IsPoGe == true ? "破格提拔" : "未破格提拔", Margin = new Thickness(0, 0, 10, 0), Width=80 ,VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        sp.Children.Add(tb);
                        tb = new TextBlock() { Text = "同意票数:" + Evaldetail.AgreeCount, Margin = new Thickness(0, 0, 5, 0), Width = 80, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        sp.Children.Add(tb);
                        tb = new TextBlock() { Text = "不同意票数:" + Evaldetail.DisAgreeCount, Margin = new Thickness(0, 0, 5, 0), Width = 80, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        sp.Children.Add(tb);
                        tb = new TextBlock() { Text = "弃权票数:" + Evaldetail.GiveUpCount, Margin = new Thickness(0, 0, 5, 0), Width = 80, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        sp.Children.Add(tb);
                        tb = new TextBlock() { Text = "不同意率:" + Evaldetail.UnAgreeRate + "%", Margin = new Thickness(0, 0, 5, 0), Width = 110, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        sp.Children.Add(tb);
                        detailsInfo.Children.Add(sp);
                    }
                }
                if (_MainInfo.PromotType == "竞争上岗" || mainInfo.PromotType == "选举")
                {
                    _client.GetCompetSpeechMainAsync("MainID=" + mainInfo.ID);
                }
                else
                {
                    _client.GetJinShen_ChooseCandidator_MainAsync("MainID=" + _MainInfo.ID);
                }

            };



            if (_MainInfo.PromotType == "竞争上岗" || mainInfo.PromotType == "选举")
            {
                _client.GetCompetSpeechMainCompleted += (sender, e) =>
                {
                    foreach (var detail in e.Result.FirstOrDefault().Details)
                    {
                        var sp = detailsInfo.Children.Where(c => ((c as StackPanel).DataContext as JinShengService.JinShen_DetailInfo).EmpBasicID == detail.EmpID).FirstOrDefault() as StackPanel;
                        var hl = new HyperlinkButton() { Content = detail.realName, Width = 120, DataContext = detail, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        hl.Click += new RoutedEventHandler(hl_Click);
                        sp.Children.Add(hl);
                    }
                    _client.GetJinShen_ChooseCandidator_MainAsync("MainID=" + _MainInfo.ID);
                };
            }
           

            _client.GetJinShen_ChooseCandidator_MainCompleted += (sender, e) =>
            {
                var chooseMain = e.Result.FirstOrDefault();
                var rate = chooseMain.FilterRate;
                foreach (UIElement ui in detailsInfo.Children)
                {
                    StackPanel panel = ui as StackPanel;
                    var detail = panel.DataContext as JinShengService.JinShen_DetailInfo;
                    var Democraticevaluation_Detail = panel.Tag as JinShengService.JinShen_Democraticevaluation_Detail;
                    var Ispass = chooseMain.Details.Where(c => c.EmpID == detail.EmpBasicID).FirstOrDefault();
                    if (Ispass != null)
                        continue;
                    if (Democraticevaluation_Detail.UnAgreeRate >= rate)
                    {
                        panel.Children.Add(new TextBlock() { Text = "民主测评未通过", VerticalAlignment = System.Windows.VerticalAlignment.Center, Foreground = new SolidColorBrush(Colors.Red) });
                    }
                    else
                    {
                        panel.Children.Add(new TextBlock() { Text = "会议讨论未通过", VerticalAlignment = System.Windows.VerticalAlignment.Center, Foreground = new SolidColorBrush(Colors.Red) });
                    }

                }
                IsWorking.Visibility = Visibility.Collapsed;
            };

            _client.GetDemocraEvalutionAsync("MainID=" + mainInfo.ID);
            _client.DownLoadFileCompleted += (sender, e) =>
            {
                if (!string.IsNullOrEmpty(e.Result))
                {
                    string serverpath = _client.InnerChannel.Via.AbsoluteUri.Replace(_client.InnerChannel.Via.AbsolutePath, "");
                    System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(serverpath + e.Result, UriKind.RelativeOrAbsolute), "_blank");
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("文件被破坏");
                }
            };
		}

        private ObservableCollection<JinShen_MainInfo> _MainInfos = null;
        public CandidatorInfoControl(ObservableCollection<JinShen_MainInfo> mainInfos)
        {
            InitializeComponent();

            IsWorking.Visibility = Visibility.Visible;
            _client = new JinShengService.JinShengServiceClient();
            _MainInfos = mainInfos;
            gggg.Visibility = Visibility.Collapsed;
            addcon.Visibility = Visibility.Visible;
            JSControl1 js1=null;
            foreach (var item in mainInfos)
            {
                js1 = new JSControl1(item);
                addcon.Children.Add(js1);
            }
            IsWorking.Visibility = Visibility.Collapsed;
        }
        SaveFileDialog openFile;
        void hl_Click(object sender, RoutedEventArgs e)
        {
            var hlb = sender as HyperlinkButton;
            JinShengService.JinShen_CompetSpeech_Detail detail = hlb.DataContext as JinShengService.JinShen_CompetSpeech_Detail;


            string extend = detail.realName.Substring(detail.realName.LastIndexOf('.'));
             
                openFile = new SaveFileDialog() { Filter = string.Format("{0}|*.{0}", extend) };
                if (openFile.ShowDialog() == true)
                {
                    IsWorking.Visibility = Visibility.Visible;
                    busy.BusyContent = "正在下载数据，请稍后...";
                    _client.DownLoadFile1Completed -= new EventHandler<JinShengService.DownLoadFile1CompletedEventArgs>(_client_DownLoadFile1Completed);
                    _client.DownLoadFile1Completed += new EventHandler<JinShengService.DownLoadFile1CompletedEventArgs>(_client_DownLoadFile1Completed);

                    _client.DownLoadFile1Async(detail.GuidName);
                }
        }
     
        void _client_DownLoadFile1Completed(object sender, JinShengService.DownLoadFile1CompletedEventArgs e)
        {
            Stream myStream;
            if ((myStream = openFile.OpenFile()) != null)
            {
                using (BinaryWriter bw = new BinaryWriter(myStream))
                {

                    bw.Write(e.Result);
                    myStream.Dispose();
                    myStream.Close();
                    SilverlightMessageBoxLibrary.Message.InfoMessage("下载完毕!");
                    IsWorking.Visibility = Visibility.Collapsed;
                }

            }
        }
        public void BeginReturn()
        {
            
        }

        private void ImageButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (_MainInfo != null)
            {
                KaoChaControl control = new KaoChaControl(_MainInfo) { Nav = this.Nav };
                Nav.ShowNextPage("考察管理", control as AsyncCtrl);
            }
            if (_MainInfos != null)
            {
                //KaoChaControl control = new KaoChaControl(_MainInfos) { Nav = this.Nav };
                //Nav.ShowNextPage("考察管理", control as AsyncCtrl);
                _client.GetJinShen_MainInfosCompleted -= new EventHandler<JinShengService.GetJinShen_MainInfosCompletedEventArgs>(_client_GetJinShen_MainInfosCompleted);
                
                _client.GetJinShen_MainInfosCompleted += new EventHandler<JinShengService.GetJinShen_MainInfosCompletedEventArgs>(_client_GetJinShen_MainInfosCompleted);
                _client.GetJinShen_MainInfosAsync(string.Format("1=1 and jsm.ID={0}", _MainInfos.First().MId), Session.LoginUser.LoginName);
         
            }
            
        }
        void _client_GetJinShen_MainInfosCompleted(object sender, JinShengService.GetJinShen_MainInfosCompletedEventArgs e)
        {
            KaoChaControl control = new KaoChaControl(e.Result[0].JinShen_MainInfos) { Nav = this.Nav };
            Nav.ShowNextPage("考察管理", control as AsyncCtrl);
        }
    }
}